#include <iostream>
#include <string>
#include <csignal>
#include <unistd.h>
#include <thread>
#include <sstream>
#include <string>
#include <sys/prctl.h>
#include <iomanip>

#include "base_android_log_utils.h"

#ifndef BASE_TRACKER_
#define BASE_TRACKER_

#define STACK_DEEP 20

static uint64_t baseIsolateInstructions;
static uint64_t baseVmInstructions;

//// --------------- only demo test -------------
static void printCurrentThreadInfo(const char *tag) {
    pid_t pid = getpid();
    pid_t tid = gettid();
    std::string threadMsg = "current pid: " + std::to_string(pid);
    threadMsg.append(", thread tic: ").append(std::to_string(tid));
    //// print thread name....
    char name[16];
    if (!prctl(PR_GET_NAME, name)) {
        threadMsg.append(", name: ").append(name);
    }
    Logger::logInfo(tag, threadMsg.c_str());
}

/// 转化函数内容，通过lr地址，以及isolate的基础地址
static std::string
transformFormat(int lineCount, uint64_t lrAddress, uint64_t isolateInstructions) {
    std::stringstream ss;
    if (lineCount < 10) {
        ss << "    #0" << lineCount << " abs ";
    } else {
        ss << "    #" << lineCount << " abs ";
    }
    ss << std::setfill('0') << std::setw(16) << std::hex << (uint64_t) lrAddress
       << " _kDartIsolateSnapshotInstructions+0x"
       << ((uint64_t) lrAddress - isolateInstructions);
    return ss.str();
}

/// base tracker .....

class BaseTracker {
protected:
    /// 类内静态数据
    static std::string staceSavePath;

public:
    virtual void registerSignalListener() = 0;

    virtual void unregisterSignalListener() = 0;

    virtual void track() = 0;

    virtual void
    setBaseIsolateInstructions(uint64_t isolateInstructions, uint64_t vmInstructions) = 0;

    virtual void setTrackStaceSavePath(std::string savePath);


    virtual ~BaseTracker();

public:
    static const char *trackerArchSpecificFunction();
};

#endif